package com.zwy.fornote.controller;

import com.github.pagehelper.PageInfo;
import com.zwy.fornote.entity.req.NewQuestionReq;
import com.zwy.fornote.entity.req.QuestionReq;
import com.zwy.fornote.entity.vo.QuestionManageVO;
import com.zwy.fornote.model.Question;
import com.zwy.fornote.service.QuestionService;
import com.zwy.fornote.util.AjaxResult;
import com.zwy.fornote.util.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *问题controller
 *
 * @author zhuyi 1205242
 * @version QuestionController 2021/3/10 20:32
 */
@RestController
@CrossOrigin
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;

    @PostMapping("/createNewQuestion")
    public AjaxResult createNewQuestion(@RequestBody NewQuestionReq req)
    {
        req.setUserId(CurrentUser.getUser().getId());
        int res = questionService.createNewQuestion(req);
        if(res>0)
        {
            return AjaxResult.ok("新建问题成功");
        }
        return AjaxResult.fail(-1,"新建问题失败");

    }

    @GetMapping("/deleteQuestion/{id}")
    public AjaxResult deleteQuestion(@PathVariable int id)
    {
        int res = questionService.deleteQuestion(id);
        if(res>0)
        {
            return AjaxResult.ok("删除成功");
        }
        return AjaxResult.fail(-1,"删除失败");

    }

    @GetMapping("/AddQuestionBrowseNum/{id}")
    public AjaxResult AddQuestionBrowseNum(@PathVariable int id)
    {
        int res = questionService.AddQuestionBrowseNum(id);
        if(res>0)
        {
            return AjaxResult.ok("增加浏览量成功");
        }
        return AjaxResult.fail(-1,"增加浏览量失败");

    }

    @PostMapping("/selectQuestion")
    public AjaxResult selectQuestion(@RequestBody QuestionReq req)
    {
        try {
            PageInfo<Question> pageInfo = questionService.selectQuestions(req);
            return AjaxResult.ok(pageInfo);
        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找问题失败",e.getMessage());
        }

    }

    @PostMapping("/manageQuestion")
    public AjaxResult selectManageQuestion(@RequestBody QuestionReq req)
    {
        try {
            PageInfo<QuestionManageVO> pageInfo = questionService.selectQuestionManageList(req);
            return AjaxResult.ok(pageInfo);
        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找管理问题列表失败",e.getMessage());
        }

    }


}
